package com.dina.saas.peccancy.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.dina.saas.peccancy.domain.RequestPeccMsg;
import com.dina.saas.peccancy.domain.ResponsePeccMsg;
import com.dina.saas.peccancy.service.DownLoadService;
import com.dina.saas.peccancy.util.Constants;

public class PeccancyController implements ServletResponseAware,
		ServletRequestAware {
	@Autowired
	private DownLoadService downLoadService;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	Logger logger = Logger.getLogger(PeccancyController.class);

	@Action("/PeccancyIntf")
	public String index() {

		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		ResponsePeccMsg peccMsg = new ResponsePeccMsg("listPecc", 1, "");
		try {
			request.setCharacterEncoding("utf-8");
			String msg = getRequestMsg();
			JSONObject json = null;
			if (StringUtils.isNotBlank(msg)) {
				try {
					json = JSONObject.fromObject(msg);
				} catch (JSONException jsone) {
					logger.error(
							"msg[ " + msg + " ]can't be formatted as json",
							jsone);
					peccMsg.setResultNote(Constants.PARAM_ERROR);
					response.getWriter().print(
							JSONObject.fromObject(peccMsg).toString());
					return null;
				}
				if (json != null && json.containsKey("params")) {
					JSONObject params = (JSONObject) json.get("params");
					if (!params.containsKey("brandId")
							|| !params.containsKey("engineNo")) {
						logger.error("msg[ "
								+ msg
								+ " ] contains key(\"brandId  engineNo\") is fail");
						peccMsg.setResultNote(Constants.PARAM_ERROR);
					} else {
						peccMsg = downLoadService.getPeccMsg((RequestPeccMsg
								.formJson(json)));
					}
				} else {
					logger.error("msg[ " + msg
							+ " ] contains key(\"params\") is fail");
					peccMsg.setResultNote(Constants.PARAM_ERROR);
				}
			} else {
				logger.error("request msg is blank!");
				peccMsg.setResultNote(Constants.PARAM_ERROR);
			}
			response.getWriter().print(
					JSONObject.fromObject(peccMsg).toString());
		} catch (IOException e) {
			logger.error("IOException!", e);
			// TODO Auto-generated catch block
		}
		return null;
	}

	private String getRequestMsg() throws IOException {
		BufferedReader br;
		StringBuilder sb = new StringBuilder();
		br = new BufferedReader(new InputStreamReader(
				(ServletInputStream) request.getInputStream()));

		String line = null;
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		return sb.toString();
	}

	public void setDownLoadService(DownLoadService downLoadService) {
		this.downLoadService = downLoadService;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;

	}

}
